/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.yy.bugzillahelp.tool;

import java.text.DecimalFormat;
import java.util.HashMap;

/**
 *
 * @author hualun-alan
 */
public class DecimalTool {

    private static HashMap<Integer, String> floatPatternMap = new HashMap<Integer, String>();
    private static HashMap<Integer, String> intPatternMap = new HashMap<Integer, String>();

    private static void initFloatPatternMap() {
        if (floatPatternMap.isEmpty()) {
            floatPatternMap.put(0, "0");
            floatPatternMap.put(1, "0.#");
            floatPatternMap.put(2, "0.##");
            floatPatternMap.put(3, "0.###");
            floatPatternMap.put(4, "0.####");
            floatPatternMap.put(5, "0.#####");
            floatPatternMap.put(6, "0.######");
            floatPatternMap.put(7, "0.#######");
            floatPatternMap.put(8, "0.########");
            floatPatternMap.put(9, "0.#########");
            floatPatternMap.put(10, "0.##########");
        }
    }

    private static void initIntPatternMap() {
        if (intPatternMap.isEmpty()) {
            intPatternMap.put(0, "0");
            intPatternMap.put(1, "0");
            intPatternMap.put(2, "00");
            intPatternMap.put(3, "000");
            intPatternMap.put(4, "0000");
            intPatternMap.put(5, "00000");
            intPatternMap.put(6, "000000");
            intPatternMap.put(7, "0000000");
            intPatternMap.put(8, "00000000");
            intPatternMap.put(9, "000000000");
            intPatternMap.put(10, "0000000000");
        }
    }

    public static String getFloatPattern(int length) {
        initFloatPatternMap();
        String pattern = floatPatternMap.get(length);
        if (pattern == null) {
            pattern = "0.####";
        }
        return pattern;
    }

    public static String getIntPattern(int length) {
        initIntPatternMap();
        String pattern = intPatternMap.get(length);
        if (pattern == null) {
            pattern = "#";
        }
        return pattern;
    }

    public static String formatToString(int value, int length) {
        DecimalFormat decimalFormat = new DecimalFormat();
        decimalFormat.applyPattern(getIntPattern(length));
        return decimalFormat.format(value);
    }

    public static void main(String[] args) {
        DecimalFormat decimalFormat = new DecimalFormat();
        System.out.println("float");
        decimalFormat.applyPattern(getFloatPattern(0));
        System.out.println(decimalFormat.format(0.123456789));
        decimalFormat.applyPattern(getFloatPattern(1));
        System.out.println(decimalFormat.format(0.123456789));
        decimalFormat.applyPattern(getFloatPattern(2));
        System.out.println(decimalFormat.format(0.123456789));
        decimalFormat.applyPattern(getFloatPattern(3));
        System.out.println(decimalFormat.format(0.123456789));
        decimalFormat.applyPattern(getFloatPattern(4));
        System.out.println(decimalFormat.format(0.123456789));
        decimalFormat.applyPattern(getFloatPattern(5));
        System.out.println(decimalFormat.format(0.123456789));
        decimalFormat.applyPattern(getFloatPattern(6));
        System.out.println(decimalFormat.format(0.123456789));
        decimalFormat.applyPattern(getFloatPattern(7));
        System.out.println(decimalFormat.format(0.123456789));
        decimalFormat.applyPattern(getFloatPattern(8));
        System.out.println(decimalFormat.format(0.123456789));
        decimalFormat.applyPattern(getFloatPattern(9));
        System.out.println(decimalFormat.format(0.123456789));
        decimalFormat.applyPattern(getFloatPattern(10));
        System.out.println(decimalFormat.format(0.123456789));

        System.out.println("int");
        decimalFormat.applyPattern(getIntPattern(0));
        System.out.println(decimalFormat.format(1234));
        decimalFormat.applyPattern(getIntPattern(1));
        System.out.println(decimalFormat.format(1234));
        decimalFormat.applyPattern(getIntPattern(2));
        System.out.println(decimalFormat.format(1234));
        decimalFormat.applyPattern(getIntPattern(3));
        System.out.println(decimalFormat.format(1234));
        decimalFormat.applyPattern(getIntPattern(4));
        System.out.println(decimalFormat.format(1234));
        decimalFormat.applyPattern(getIntPattern(5));
        System.out.println(decimalFormat.format(1234));
        decimalFormat.applyPattern(getIntPattern(6));
        System.out.println(decimalFormat.format(1234));
        decimalFormat.applyPattern(getIntPattern(7));
        System.out.println(decimalFormat.format(1234));
        decimalFormat.applyPattern(getIntPattern(8));
        System.out.println(decimalFormat.format(1234));
        decimalFormat.applyPattern(getIntPattern(9));
        System.out.println(decimalFormat.format(1234));
        decimalFormat.applyPattern(getIntPattern(10));
        System.out.println(decimalFormat.format(1234));

    }
}
